home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSDEBUG.C < prev    next >
C/C++ Source or Header  |  1993-08-24  |  6KB  |  194 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsdebug.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to assist in debugging. 
  25. //    It contains array used to store the current file and line number for a 
  26. //    debugging operation.
  27. //
  28. //    The code in this module should be written entirely in C. 
  29. //    Do not use any C++ constructs.
  30. //
  31. //    This module is portable to:
  32. //        DOS 3.X+
  33. //        MS Windows 3.X+
  34. //        OS/2 2.X+
  35. //        OS/2 2.0 PM
  36. //        SCO UNIX.
  37. //
  38. //    The following compilers are supported:
  39. //        MSC 6.0A
  40. //        MSC/C++ 7.0
  41. //        Borland C++ 3.1 for DOS
  42. //        Borland C++ 1.0 for OS/2 2.X
  43. //        SCO UNIX cc
  44. //
  45. //----------------------------------------------------------------------------
  46. #include <bs.h>
  47.  
  48. //----------------------------------------------------------------------------
  49. //    Globals
  50. //----------------------------------------------------------------------------
  51. static SIZET acLine[MAX_THREADS];
  52. static PSZ apszModule[MAX_THREADS];
  53.  
  54.  
  55. //----------------------------------------------------------------------------
  56. //   Description:    Get context string.
  57. //                          This string includes process id, thread id, date/time, and
  58. //                        module/line number.
  59. //    Parameters: pszBuf        Buffer for context string. Should be at least
  60. //                                        160 bytes in length.
  61. //                        fShort        Short context?
  62. //       Returns:    Pointer to buffer
  63. //----------------------------------------------------------------------------
  64. PSZ FN DebugContext(PSZ pszBuf, BOOL fShort)
  65. {
  66.     time_t now = time(NULL);
  67.     struct tm *tm = localtime(&now);
  68.     TID tid = THREADID - 1;
  69.  
  70.     if (!DebugMode())
  71.         {
  72.       pszBuf[0] = '\0';
  73.       }
  74.     else if (fShort)
  75.        {
  76.         sprintf(pszBuf,
  77.             "PID=%08lX TID=%-4ld %2d:%02d:%02d %2d/%02d/%4d\n"
  78.             "Module: %-40.40s Line: %u\n",
  79.             (ULONG)Pid(), (LONG)THREADID,
  80.             tm->tm_hour, tm->tm_min, tm->tm_sec,
  81.             tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
  82.             apszModule[(SIZET)tid], acLine[tid]);
  83.         }
  84.     else
  85.         {
  86.         sprintf(pszBuf,
  87.             "PID=%08lX\n"
  88.             "TID=%-4ld\n"
  89.             "%d:%02d:%02d %2d/%02d/%4d\n"
  90.             "Module: %-.40s\n"
  91.             "Line: %u\n",
  92.             (ULONG)Pid(), (LONG)THREADID,
  93.             tm->tm_hour, tm->tm_min, tm->tm_sec,
  94.             tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
  95.             apszModule[(SIZET)tid], acLine[tid]);
  96.       }
  97.     return pszBuf;
  98. }
  99.  
  100.  
  101. //----------------------------------------------------------------------------
  102. //   Description:    Get line number of last debug operation.
  103. //    Parameters:
  104. //       Returns:    Line number
  105. //----------------------------------------------------------------------------
  106. SIZET FN DebugLine(void)
  107. {
  108.     TID tid = THREADID - 1;
  109.     return acLine[(SIZET)tid];
  110. }
  111.  
  112.  
  113. //----------------------------------------------------------------------------
  114. //   Description:    Get debug mode
  115. //    Parameters:    
  116. //       Returns:    TRUE if debug mode is active.
  117. //----------------------------------------------------------------------------
  118. BOOL FN_E DebugMode(void)
  119. {
  120. static BOOL fDebug = FALSE;
  121. static BOOL fInit = FALSE;
  122.  
  123.     if (!fInit)
  124.         {
  125.         fDebug = EnvCheck("__DEBUGGER__", "YES");
  126.         fInit = TRUE;
  127.         Log(LOG_HIGH, "Debug mode %s enabled.", (fDebug ? "IS": "IS NOT"));
  128.         }
  129.     return fDebug;
  130. }
  131.  
  132.  
  133. //----------------------------------------------------------------------------
  134. //   Description:    Get module name of last debug operation
  135. //    Parameters:    
  136. //       Returns:    Pointer to module name
  137. //----------------------------------------------------------------------------
  138. PCSZ FN DebugModule(void)
  139. {
  140.     TID tid = THREADID - 1;
  141.     return (PCSZ)(apszModule[(SIZET)tid] ? apszModule[(SIZET)tid]: "unknown");
  142. }
  143.  
  144.  
  145. //----------------------------------------------------------------------------
  146. //   Description:    Set location of debug information
  147. //    Parameters:    pszModule        Module name
  148. //                        cLine                Line number
  149. //       Returns:    FALSE
  150. //----------------------------------------------------------------------------
  151. BOOL FN_E DebugSetLocation(PSZ pszModule, SIZET cLine)
  152. {
  153.     TID tid = THREADID - 1;
  154.     PSZ psz;
  155.                                                     // Skip path specifier
  156.     if ((psz = strchr(pszModule, PATH_SEP)) != NULL)
  157.         pszModule = psz+1;
  158.  
  159. #if OS_UNIX==0
  160.     if ((psz = strchr(pszModule, DRIVE_SEP)) != NULL)
  161.         pszModule = psz+1;
  162.  
  163.     strupr(pszModule);                        // Upper case PC names
  164. #endif
  165.  
  166.     acLine[(SIZET)tid] = cLine;
  167.     apszModule[(SIZET)tid] = pszModule;
  168.     return FALSE;
  169. }
  170.  
  171.  
  172. //----------------------------------------------------------------------------
  173. //   Description:    Run standard test suite
  174. //    Parameters:    sTest        Test to run.
  175. //                                        0        Run all default tests (except).
  176. //       Returns:    TRUE if successful.
  177. //----------------------------------------------------------------------------
  178. #if COMPILE_TEST
  179. BOOL FN DebugTest(SHORT sTest)
  180. {
  181.     CHAR szContext[160];
  182.  
  183.     NOTUSED(sTest);
  184.     DebugSetLocation(__FILE__,__LINE__);
  185.     Output("Location %s,%u\n", DebugModule(), DebugLine());
  186.     Output("Context Long:\n%s", DebugContext(szContext, FALSE));
  187.     Output("Context Short:\n%s", DebugContext(szContext, TRUE));
  188.     return TRUE;
  189. }
  190. #endif
  191. //----------------------------------------------------------------------------
  192. //------------------------------- End of File --------------------------------
  193. //----------------------------------------------------------------------------
  194.